package com.qf.service.impl;

import com.qf.entity.*;
import com.qf.mapper.ClassroomMapper;
import com.qf.mapper.StudentMapper;
import com.qf.mapper.SubjectMapper;
import com.qf.service.StudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassroomMapper classroomMapper;
    @Autowired
    private SubjectMapper subjectMapper;

    @Override
    public Student findById(int id) {
        return studentMapper.findById(id);
    }

    @Override
    public List<Student> selectAll() {
        return studentMapper.selectAll();
    }

    @Override
    public List<Student> findByKeyword(HashMap<String, Object> map) {
        return studentMapper.findByKeyword(map);

    }

    @Override
    public int addStudent(Student student) {
        return studentMapper.addStudent(student);

    }

    @Override
    public int deleteStudent(HashMap<String, Object> map) {
        return studentMapper.deleteStudent(map);
    }

    @Override
    public int updateStudent(HashMap<String, Object> map) {
        return studentMapper.updateStudent(map);
    }

    @Override
    public int batchDeleteStudent(List<Integer> ids) {
        return studentMapper.batchDeleteStudent(ids);
    }

    @Override
    public List<StudentVO> findAllStudentAndClassroom() {
        return studentMapper.findAllStudentAndClassroom();
    }

    @Override
    public StudentVO findStudentAndAllSubjectByStuid(int id) {
        return studentMapper.findStudentAndAllSubjectByStuid(id);
    }

    @Override
    public StudentVO findStudentAndAllSubjectAndTeacherByStuid(int id) {
        return studentMapper.findStudentAndAllSubjectAndTeacherByStuid(id);
    }


    //查询学生信息以及关联的教室信息
    @Override
    public List<StudentVO> findAllStudentAndClassroomV2() {

        // 创建最终的结果集合,存储学生信息及教室信息
        List<StudentVO> list = new ArrayList<>();

        // 查询所有学生
        List<Student> studentList = studentMapper.selectAll();
        System.out.println("studentList = " + studentList);


        // 遍历得到所有学生对象--对每一个对象的cid进行查询
        studentList.forEach(student -> {
            // 再根据学生对象中的教室id查询对应的教室
            Classroom classroom = classroomMapper.findClassroomById(student.getCid());
            // 创建最终封装数据的对象
            StudentVO studentVO = new StudentVO();

            // BeanUtils是spring框架提供的工具类,copyProperties方法用与拷贝对象属性
            // 此处是将student对象中的属性赋值到studentVO中
            BeanUtils.copyProperties(student, studentVO);
            // studentVO中存储教室信息
            studentVO.setClassroom(classroom);
            // 将每一个StudentVO添加到List集合
            list.add(studentVO);
        });
        return list;
    }

    @Override
    public StudentVO findStudentAndAllSubjectByStuidV2(int id) {
        //创建最后得集合List--存储学生信息与学科信息
        List<StudentVO> list = new ArrayList<>();
        // 创建集合准备存储每个学科对象
        List<Subject> subjectList = new ArrayList<>();
        //通过id找到学生对象
        Student student = studentMapper.findById(id);
        //通过学生的id找到所有的subId--使用list存储
        List<Integer> subIdList = studentMapper.findAllSubjectId(id);
        //根据subId在sub表找到sub对象
        subIdList.forEach(subId -> {
            // 再根据学科id获得每一个学科对象
            Subject subject = subjectMapper.findSubjectById(subId);
            // 存储学科对象
            subjectList.add(subject);
        });

        // 封装数据到VO
        StudentVO studentVO = new StudentVO();
        BeanUtils.copyProperties(student, studentVO);
        //创建一个subVO类装subject对象
        ArrayList<SubjectVO> subjectVOS = new ArrayList<>();
        subjectList.forEach(subject -> {
            SubjectVO subjectVO = new SubjectVO();
            //Spring独有的工具类装进去,将subsubject,复制到单个subjectVO中
            BeanUtils.copyProperties(subject, subjectVO);
            //将单个对象加入到subjectVOS
            subjectVOS.add(subjectVO);
        });

        studentVO.setSubjectList(subjectVOS);
        return studentVO;
    }


    //使用名字查询学生信息
    @Override
    public Student findStudentByUsername(HashMap<String, Object> map) {
        System.out.println("map = " + map);
        return studentMapper.findStudentByUsername(map);
    }
}


